#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry
# from jarryLibs.JQt.InitQtModule import *
from JTK.jQt.InitQtModule import *
import importlib


class ThreadClass(QThread):
    log = Signal(str, str, str)

    def __init__(self, name, fix=False, parent=None):
        super(ThreadClass, self).__init__(parent)
        self.class_path_list = name
        self.fix = fix
        self.parent = parent

    def run(self):
        self.parent.checkInfo_pb.setEnabled(False)

        for class_path in self.class_path_list:
            class_name = class_path.split('.')[-1]
            self.log.emit(class_name, '', 'working')
            # 使用maya内核的多线程处理程序，直接使用python的多线程会报错
            import maya.utils
            # print class_path
            result = maya.utils.executeInMainThreadWithResult(self.check, class_path)
            # for log in buffer:

            log = result.strip()
            if self.fix:
                if 'Error:' in log:
                    self.log.emit(class_name, log, 'fixFailed')
                elif 'Succeed:' in log:
                    self.log.emit(class_name, log, 'fixSucceeded')
            else:
                if 'Error:' in log:
                    self.log.emit(class_name, log, 'checkFailed')
                elif 'Succeed:' in log:
                    self.log.emit(class_name, log, 'checkSucceeded')
                elif 'Warning:' in log:
                    self.log.emit(class_name, log, 'checkWarning')
            # cmds.waitCursor(st=0)

        self.parent.checkInfo_pb.setEnabled(True)

    def check(self, class_path):
        # print class_path
        command = __import__('checkScript.{0}'.format(class_path), fromlist=[''])
        # reload(command)
        importlib.reload(command)
        class_name = class_path.split('.')[-1]
        if self.fix:
            command_str = 'command.{0}().{1}()'.format(class_name, 'fix')
        else:
            command_str = 'command.{0}().{1}()'.format(class_name, 'check')
        result = eval(command_str)
        return result
